package service.local.beans;

import java.util.List;

import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Stateless;

import model.Role;
import repository.RoleRepository;
import repository.exceptions.RepositoryException;
import service.ServiceException;
import service.local.RoleService;
@Stateless (name = "RoleService", mappedName = "RoleService")
@DependsOn ({"RoleRepository"})
public class RoleServiceBean implements RoleService {
	
	@EJB
	RoleRepository repo;
	
	@Override
	public Role insertRole(Role role){
		try {
			return repo.save(role);
		} catch (final RepositoryException ex) {
			
		}
		return null;
	}

	@Override
	public Role updateRole(Role role){
		try {
			if (repo.getById(role.getId()) != null) {
				return repo.merge(role);
			} else {
			
			}
		} catch (final RepositoryException ex) {
			
		}
		return null;
	}

	@Override
	public void deleteRole(Role role){
		try {
			if (repo.getById(role.getId()) != null) {
				repo.delete(role);
			} else {
			
			}
		} catch (final RepositoryException ex) {
			
		}
		
	}

	@Override
	public List<Role> getAll(){
		try {
			return repo.getAll();
		}catch(final RepositoryException ex) {
		
		}
		return null;
	}

	@Override
	public Role getRoleByName(String roleAsString) {
		try {
			return repo.geRoleByName(roleAsString);
		}catch(final RepositoryException ex) {
		
		}
		return null;
	}

	@Override
	public List<Role> getRoleByUserName(String user) {
		try {
			return repo.getRolesByUserName(user);
		}catch(final RepositoryException ex) {
		
		}
		return null;
	}



}
